<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Home</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    html,
    body {
      width: 100vw;
      height: 100vh;
    }

    body {
      background: url("/images/index_bg.jpg") no-repeat center center;
      background-size: cover;
    }

    nav {
      display: flex;
      justify-content: center;
    }

    a {
      text-decoration: none;
      padding: 10px;
      display: block;
      color: #fff;
      background-color: rgb(214, 92, 35);
      text-align: center;
    }

    form {
      width: 30vw;
      position: fixed;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      padding: 20px;
      background-color: #fff;
      border-radius: 30px;
      box-shadow: 2px 2px 2px 1px #ccc;
    }

    form input {
      outline: none;
      border: 2px solid #333;
      background: none;
      font-size: 20px;
      color: #ccc;
      padding: 10px;
      margin: 6px 0;
    }

    ul {
      /* display: flex; */
      justify-content: space-around;
      list-style: none;
      position: fixed;
      bottom: 5%;
      width: 100%;
      display: none;
    }

    ul li {
      cursor: pointer;
      padding: 10px 20px;
      background-color: #f40;
      border-radius: 5%;
      color: #fff;
    }
  </style>
</head>

<body>
  <nav>
    <a href="/home">欢迎 <span></span></a>
  </nav>

  <form>
    <p>
      <label>name：</label>
      <input type="text">
    </p>
    <p>
      <label>sex：</label>
      男 <input type="radio" name="a" checked> 女<input type="radio" name="a">
    </p>
    <p>
      <label>age：</label>
      <input type="number">
    </p>
    <p>
      <label>mail：</label>
      <input type="text">
    </p>
    <p>
      <label>phone：</label>
      <input type="text">
    </p>
  </form>
  <ul>
    <li>增加</li>
    <li>删除</li>
    <li>修改</li>
  </ul>
  <script>
    let uname = localStorage.getItem('uname');
    let input = document.querySelectorAll('form p input');
    let ul = document.getElementsByTagName('ul')[0]
    if (!uname) {
      alert('请登陆')
      window.location.href = '/login';
    }
    document.querySelector("nav a span").innerText = uname;

    // 获取数据
    let xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function () {
      if (xhr.readyState == 4 && xhr.status == 200) {
        let data = JSON.parse(xhr.responseText)
        if (xhr.responseText == '6') {
          console.log('未知错误');
        } else if (xhr.responseText == '5') {
          window.location.href = '/register'
        } else if (data.pid === 0 || data.pid) {
          init(data);
        }
      }
    }

    xhr.open('GET', '/index_getdata?uname=' + uname, true);
    xhr.send()


    function init(data) {
      input[0].value = data.name;
      if (data.sex) {
        input[2].checked = true;
      }
      input[3].value = data.age
      input[4].value = data.mail
      input[5].value = data.phone

      let lock = true;
      for (const key in data) {
        if (data[key] === 0 || data[key] === '') {
          lock = false;
          break;
        }
      }

      if(lock) {
        ul.removeChild(ul.children[0])
      }

      ul.style.display = 'flex';
    }
  </script>
</body>

</html>